#include "stdafx.h"
#include "CombatSysM.h"
#include "Combat/CombatSkill.h"
#include "Entity/EntityIf.h"

#include "States/CombatState_Casting.h"
#include "States/CombatState_Charging.h"
#include "States/CombatState_CoolDown.h"
#include "States/CombatState_Forbidden.h"
#include "States/CombatState_Peace.h"
#include "States/CombatState_Ready.h"
#include "States/CombatState_Standby.h"

bool _g_bisCalculator = false;


//
//	Combat Data For Monster
//

CombatSysM::CombatSysM()
:m_wpTheMainSkill(NULL), m_nSkillCnt(0)
{
	m_nFaction = Combat::FC_Monster;

#ifdef _DEBUG
	MoR = 0;
#endif
	for (int i=0; i<cnt_SpecialSkillM; i++)
	{
		m_wpSkill[i] = NULL;
	}
}


void
CombatSysM::updateMP(int nAffectValue)
{
	//
	//	Monster don't need to calculate MP
}

void
CombatSysM::updateMPBottleValue(int nAffectValue)
{
	//
	//	Monster don't need to calculate MP
}

bool
CombatSysM::onInit(IGeneCreateData& createData)
{
	if (__super::onInit(createData))
	{
		return initState(Combat::CBS_Casting, new CombatState_Casting) &&
			initState(Combat::CBS_Ready, new CombatState_Ready) &&
			initState(Combat::CBS_Forbidden, new CombatState_Forbidden) &&
			initState(Combat::CBS_Peace, new CombatState_Peace) &&
			initState(Combat::CBS_CoolDown,new CombatState_CoolDown) &&
			initState(Combat::CBS_Charging, new CombatState_Charging) &&
			initState(Combat::CBS_Standby, new CombatState_Standby);
	}
	else
	{
		return false;
	}
}

bool
CombatSysM::isCalculator()const
{
	return	_g_bisCalculator;
}

bool
CombatSysM::TryCastNewSpell()
{
	CombatSkill* pSkill = NULL;

	//
	//	Temp code here
	if (!hasFlg(Combat::FIdx_Daze) &&
		m_nSkillCnt>0)
	{
		int nRand = rand()%100;
		if (nRand<70)
		{
			pSkill = m_wpSkill[0];
		}
		else if (nRand<85)
		{
			pSkill = m_wpSkill[1];
		}
		else if (nRand<95)
		{
			pSkill = m_wpSkill[2];
		}
		else
		{
			pSkill = m_wpSkill[3];
		}
	}

	if (pSkill==NULL)
	{
		pSkill = m_wpTheMainSkill;
	}

	if (pSkill!=NULL)
	{
		IEntity* pTarget = getTargetCE();
		//
		//	Check whether there is enough energy for casting the next spell
		//
		if ( pTarget!=NULL &&
			getMP()>=pSkill->m_nEnergeCost)
		{
			return sendMsg_CbtStart(*pTarget, *pSkill);
		}
		else
		{
			return false;
		}
	}

	return false;
}

void
CombatSysM::onEvent_CastPrep(IEvt& evt)
{
	GECastPrep* pGE = dynamic_cast<GECastPrep*>(&evt);
	if (pGE!=NULL)
	{
		if (pGE->m_nType==GECastPrep::Main)
		{
			m_wpTheMainSkill = pGE->m_wpSkill;
			pGE->setDataY(CombatResult::Succeed);
		}
		else
		{
			if (m_nSkillCnt<cnt_SpecialSkillM)
			{
				m_wpSkill[m_nSkillCnt++] = pGE->m_wpSkill;
				pGE->setDataY(CombatResult::Succeed);
			}
			else
			{
				pGE->setDataY(CombatResult::Busy);
			}
		}
	}
}

#ifdef _DEBUG
const char*
CombatSysM::getCombatTag()const
{
	return "M";
}
#endif
